package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.EmploymentDao;
import com.example.domain.Employment;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.EmploymentService;
import com.example.tool.DateTool;

/**
 * @author 袁小弘
 * @describe 就业登记服务层接口实现
 * @date 2020-01-01
 */
@Service
public class EmploymentServiceImpl implements EmploymentService {

    @Autowired
    EmploymentDao employmentDao;

    /**
     * @describe 添加就业登记信息
     */
    @Override
    public Result addEmployment(Employment employment) {
        if (employment != null) {
            //获取系统当前时间作为记录创建时间
            employment.setRecordTime(DateTool.getTimestamp());
        }
        //调用JPA保存实体方法
        Employment employment1 = employmentDao.save(employment);

        if (employment1 != null) {
            return ResultFactory.buildSuccessResult(employment1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新就业登记信息
     */
    @Override
    public Result updateEmployment(Employment employment) {
        if (employment != null) {
            //获取系统当前时间作为记录更新时间
            employment.setUpdateTime(DateTool.getTimestamp());
        }
        //调用JPA保存并刷新实体方法
        Employment employment1 = employmentDao.saveAndFlush(employment);

        if (employment1 != null) {
            return ResultFactory.buildSuccessResult(employment1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号删除该就业登记信息
     */
    @Override
    public Result deleteEmploymentById(String employmentId) {

        int row = employmentDao.deleteByEmploymentId(Integer.parseInt(employmentId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有就业登记信息
     */
    @Override
    public Result queryAllEmployment() {

        List<Employment> list = employmentDao.findAll();

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询该就业登记信息
     */
    @Override
    public Result queryEmploymentById(String employmentId) {

        List<Employment> list = employmentDao.findByEmploymentId(Integer.parseInt(employmentId));

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加就业登记信息
     */
    @Override
    public Result addEmploymentByExcel(List<Employment> list) {
        // TODO 通过Excel批量添加就业登记信息
        List<Employment> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = employmentDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据就业登记编号批量删除就业登记信息
     */
    @Override
    public Result deleteEmploymentByIds(Map<String, String> ids) {
        // TODO 根据就业登记编号批量删除就业登记信息
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = employmentDao.deleteByEmploymentIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 就业登记信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        // TODO 就业登记信息模糊查询
        List<Map<String, Object>> list = null;

        if (info[0].equals("status")) {

            list = employmentDao.findByStatusLike(info[1]);

        } else if (info[0].equals("enterPrise")) {

            list = employmentDao.findByEnterPriseLike(info[1]);

        } else if (info[0].equals("entryTime")) {

            list = employmentDao.findByEntryTimeLike(info[1]);

        } else if (info[0].equals("dimissionTime")) {

            list = employmentDao.findByDimissionTimeLike(info[1]);

        } else if (info[0].equals("userName")) {

            list = employmentDao.findByUserNameLike(info[1]);

        } else if (info[0].equals("nickName")) {

            list = employmentDao.findByNickNameLike(info[1]);

        }

        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有就业登记和用户信息
     */
    @Override
    public Result queryAllEmploymentAndUsersInfo() {
        // TODO 查询所有就业登记和用户信息
        List<Map<String, Object>> list = employmentDao.queryAllEmploymentAndUsersInfo();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

}
